package com.ponhu.ea2.goodsCenter.provider.facade.impl;

import com.ponhu.ea2.goodsCenter.provider.mapper.GoodsInfoForAppMapper;
import com.ponhu.ea2.goodsCenter.provider.mapper.GoodsPriceForAppMapper;
import com.ponhu.ea2.goodsCenter.provider.mapper.InventoryMapper;
import com.ponhu.ea2.service.facade.GoodsInfoForAppService;
import com.ponhu.ea2.vo.GoodsInfoForAppVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author luyu
 * @create 2021-09-07 14:19
 *
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = GoodsInfoForAppService.class, timeout = 6000)
public class GoodsInfoForAppServiceImpl implements GoodsInfoForAppService {

    @Resource
    GoodsInfoForAppMapper goodsInfoForAppMapper;

    @Resource
    GoodsPriceForAppMapper goodsPriceForAppMapper;

    @Resource
    InventoryMapper inventoryMapper;

    @Override
    public GoodsInfoForAppVo getGoodsInfoByCode(String goodsCode) {
        GoodsInfoForAppVo goodsInfoForAppVo = null;
        try {
            // 查询胖总管的数据,这里官网售价取胖总管的建议价
            goodsInfoForAppVo = goodsInfoForAppMapper.getGoodsInfoForAppVoByCode(goodsCode);
            //goodsInfoForAppVo = inventoryMapper.getGoodsInfoByCodeForApp(goodsCode);
            if(ObjectUtils.isEmpty(goodsInfoForAppVo)){
                return new GoodsInfoForAppVo();
            }
            log.info("胖总管查询到的商品信息为:"+goodsInfoForAppVo.toString());

            // 查询同行价,获取当前时间180天之内的同行价的平均值
            // 定义成色值
            Map<String, String > qualityMap = new HashMap<String, String>(){{
                put("99新","10.0");
                put("98新","9.8");
                put("95新","9.5");
                put("9新","9.0");
                put("85新","8.5");
                put("8新","8.0");
            }};
            BigDecimal tradePrice = getTradePrice(qualityMap.get(
                    goodsInfoForAppVo.getQualityValue()),
                    goodsInfoForAppVo.getSeries(),
                    goodsInfoForAppVo.getBrandName());
            if(!ObjectUtils.isEmpty(tradePrice)){
                goodsInfoForAppVo.setTradePrice(tradePrice.intValue());
            }
            // 查询价格趋势,半年内每个月同行价平均值
            Map<String, Integer> priceTrend = getPriceTrend(
                    qualityMap.get(goodsInfoForAppVo.getQualityValue()),
                    goodsInfoForAppVo.getSeries(),
                    goodsInfoForAppVo.getBrandName());
            if(!CollectionUtils.isEmpty(priceTrend)){
                goodsInfoForAppVo.setPriceTrend(priceTrend);
            }

        } catch (Exception e) {
            e.printStackTrace();
            goodsInfoForAppVo = new GoodsInfoForAppVo();
        }
        return goodsInfoForAppVo;
    }

    // 查询同行价
    public BigDecimal getTradePrice(String quality,String series,String brandName){
        // 获取180天之前的时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -180);
        Date m3 = c.getTime();
        String mon3 = format.format(m3);
        BigDecimal tradePrice = goodsPriceForAppMapper.getTradePrice(mon3, quality, series,brandName);
        return tradePrice;
    }

    // 查询价格趋势
    public Map<String,Integer> getPriceTrend(String quality,String series,String brandName){
        // 获取6个月之前的时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -6);
        Date m3 = c.getTime();
        String mon3 = format.format(m3);

        List<Map<String,String>> tradePriceMap = goodsPriceForAppMapper
                .getPriceTrend(mon3, quality, series, brandName);
        Map<String, Integer> map = new HashMap<>();
        if(tradePriceMap.isEmpty()){
            return map;
        }
        for (Map<String, String> stringBigDecimalMap : tradePriceMap) {
            String ukey = null;
            String price = null;
            for (Map.Entry<String, String> entry : stringBigDecimalMap.entrySet()) {
                if("sj".equals(entry.getKey())){
                    ukey = entry.getValue();
                }else {
                    price = entry.getValue();
                }
            }
            map.put(ukey,new BigDecimal(price).intValue());
        }
        return map;
    }
}
